package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginRequired;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequestMapping("/travel")
@Controller
public class TravelController {
    @Value("${file.dir}")
    private String dir;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private ITravelCommentService travelCommentService;
    @Reference
    private IStrategyDetailService strategyDetailService;

    @RequestMapping("")
    public String list(@ModelAttribute("qo")TravelQuery qo, Model model){
        model.addAttribute("pageInfo", travelService.query(qo));
        return "travel/list";
    }
    @RequestMapping("/detail")
    public String detail(Long id, Model model){
        Travel travel = travelService.selectContainContentByTravelId(id);
        model.addAttribute("detail", travel);
        Long destId = travel.getDest().getId();
        //toasts
        List<Destination> list = destinationService.toasts(travel.getDest().getId());
        model.addAttribute("toasts", list);
        model.addAttribute("dest", destinationService.selectByDestId(destId));
        //共享相关攻略,相关游记
        model.addAttribute("sds", strategyDetailService.selectReplyTop3ByDestId(destId));
       // model.addAttribute("ts", travelService.select)
        //共享评论
        model.addAttribute("list", travelCommentService.findByTravelId(id));
        return "travel/detail";
    }
    @RequestMapping("input")
    @LoginRequired
    public String input(Long id, Model model){
        model.addAttribute("dests", destinationService.selectAllDests());
        return "travel/input";
    }
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic){
        System.out.println(dir +"/"+ UploadUtil.upload(pic, dir));
        return UploadUtil.upload(pic, dir);
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @LoginUser UserInfo userInfo){
        //新增的话设置作者为当前登录用户
        if(travel.getId() == null){
            travel.setAuthor(userInfo);
        }
        return travelService.saveOrUpdate(travel);
    }
    @RequestMapping("/commentAdd")
    public String commentAdd(TravelComment comment, int floor, Model model, @LoginUser UserInfo userInfo){
        //页面共享回floor楼层,这里就简单的自增
        model.addAttribute("floor", ++floor);
        //将评论者信息设置进去
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        //保存评论,并且返回改评论共享到页面
        model.addAttribute("c", travelCommentService.save(comment));
        return "travel/commentTpl";
    }
}
